package com.ichir.projects.uiflow.internal.factories;

import java.util.HashMap;
import java.util.Map;

import com.ichir.projects.uiflow.ApplicationContext;
import com.ichir.projects.uiflow.definition.FlowType;
import com.ichir.projects.uiflow.definition.IFlowPart;
import com.ichir.projects.uiflow.ui.api.IUiPart;

public abstract class UiPartFactory {

	private static HashMap<FlowType, UiPartFactory> factories = new HashMap<FlowType, UiPartFactory>();
	
	static {
		factories.put(FlowType.ACTION, new UiActionFactory());
		factories.put(FlowType.VIEW, new UiViewFactory());
	}
	
	/**
	 * 
	 * @param flowPart
	 * @param context
	 * @param attributes
	 * @return
	 */
	public static IUiPart getUiPart(IFlowPart flowPart, ApplicationContext context, Map<String, Object> attributes) {
		if (flowPart == null) {
			return null;
		}
		try {
			System.out.println(">> get Ui Part for flow part "+flowPart.getId()+", classname "+flowPart.getUiPartClassname()+", type "+flowPart.getType());
			// get factory
			return factories.get(flowPart.getType()).getPart(flowPart, context, attributes);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	protected abstract IUiPart getPart(IFlowPart flowPart, ApplicationContext context, Map<String, Object> attributes) throws Exception;
	
}
